/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Daerah;

import Bakul.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author gatz
 */
public class FormDaerah extends javax.swing.JFrame {

    /**
     * Creates new form KategoriForm2
     */
    public FormDaerah() {
        initComponents();
        IdDaerahText.requestFocus();
        SimpanButton.setEnabled(false);
        tampilDaerah();
//        tampilIdDaerah();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        IdDaerahText = new javax.swing.JTextField();
        NamaDaerahText = new javax.swing.JTextField();
        SimpanButton = new javax.swing.JButton();
        HapusButton = new javax.swing.JButton();
        KeluarButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelDaerah = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(0, 102, 102));

        jLabel1.setBackground(new java.awt.Color(255, 102, 0));
        jLabel1.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Daftar Daerah");

        jLabel4.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel4.setText("ID Daerah");

        jLabel5.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        jLabel5.setText("Nama Daerah");

        IdDaerahText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IdDaerahTextActionPerformed(evt);
            }
        });
        IdDaerahText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                IdDaerahTextKeyReleased(evt);
            }
        });

        NamaDaerahText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                NamaDaerahTextKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(IdDaerahText)
                    .addComponent(NamaDaerahText, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(IdDaerahText))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NamaDaerahText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        SimpanButton.setText("Simpan");
        SimpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SimpanButtonActionPerformed(evt);
            }
        });

        HapusButton.setText("Hapus");
        HapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HapusButtonActionPerformed(evt);
            }
        });

        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SimpanButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(32, 32, 32)
                .addComponent(HapusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(135, 135, 135)
                .addComponent(KeluarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SimpanButton)
                    .addComponent(HapusButton)
                    .addComponent(KeluarButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(0, 102, 102));

        jLabel2.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Daftar Daerah");

        TabelDaerah.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Id Daerah", "Nama Daerah"
            }
        ));
        TabelDaerah.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TabelDaerahMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TabelDaerah);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void IdDaerahTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IdDaerahTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_IdDaerahTextActionPerformed

    private void IdDaerahTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_IdDaerahTextKeyReleased
        // TODO add your handling code here:
//        try {
//            // TODO add your handling code here:
//            String kodeKategori = KodeKategoriText.getText().trim();
//            String namaKat = KategoriKontrol.getKoneksiKategori().cariKategori(kodeKategori);
//            NamaKategoriText.setText(namaKat);
//        } catch (SQLException ex) {
//            Logger.getLogger(KategoriForm.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_IdDaerahTextKeyReleased

    private void NamaDaerahTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_NamaDaerahTextKeyReleased
        // TODO add your handling code here:
        SimpanButton.setEnabled(true);
    }//GEN-LAST:event_NamaDaerahTextKeyReleased

    private void SimpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimpanButtonActionPerformed
        // TODO add your handling code here:
        String id = IdDaerahText.getText();
        String nama = NamaDaerahText.getText();

        Daerah daerah = new Daerah();
        daerah.setIdDaerah(id);
        daerah.setNamaDaerah(nama);

        DaerahKontrol daerahKontrol;
        try {
            daerahKontrol = DaerahKontrol.getKoneksiDaerah();
            if (SimpanButton.getText().equalsIgnoreCase("Simpan")) {
                // ini pake method biasa
                daerahKontrol.tambahDaerah(daerah);

                // ini pake Colable Stored Procedure
//                bakulKontrol.tambahBakulCall(bakul);
                JOptionPane.showMessageDialog(this, "Data dengan nama " + nama + " berhasil disimpan ");
                IdDaerahText.setText(" ");
                NamaDaerahText.setText(" ");
                tampilDaerah();
            } else {
                daerahKontrol.updateDaerah(daerah);
                JOptionPane.showMessageDialog(this, "Data Berhasil diupdate = " + nama);
                IdDaerahText.setText(" ");
                NamaDaerahText.setText(" ");
                tampilDaerah();
            }
        } catch (SQLException ex) {
            Logger.getLogger(FormDaerah.class.getName()).log(Level.SEVERE, null, ex);
        }
        SimpanButton.setText("Simpan");
//        tampilIdDaerah();

    }//GEN-LAST:event_SimpanButtonActionPerformed

    private void HapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HapusButtonActionPerformed
        // TODO add your handling code here:
        Daerah daerah = new Daerah();
        daerah.setIdDaerah(IdDaerahText.getText());
        daerah.setNamaDaerah(NamaDaerahText.getText());
        DaerahKontrol daerahKontrol;
        try {
            daerahKontrol = DaerahKontrol.getKoneksiDaerah();

            daerahKontrol.deleteDaerah(daerah);
            JOptionPane.showMessageDialog(null, "Data daerah dengan id " + IdDaerahText.getText()
                    + "\nberhasil didelete !!");
            IdDaerahText.setText("");
            NamaDaerahText.setText("");
        } catch (SQLException ex) {
            Logger.getLogger(FormDaerah.class.getName()).log(Level.SEVERE, null, ex);
        }
        tampilDaerah();
        SimpanButton.setText("Simpan");
    }//GEN-LAST:event_HapusButtonActionPerformed

    private void TabelDaerahMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelDaerahMouseClicked
        // TODO add your handling code here:
        int baris = TabelDaerah.getSelectedRow();
        String kode = TabelDaerah.getValueAt(baris, 0).toString();
        String nama = TabelDaerah.getValueAt(baris, 1).toString();

        IdDaerahText.setText(kode);
        NamaDaerahText.setText(nama);

        SimpanButton.setEnabled(true);
        SimpanButton.setText("Update");

    }//GEN-LAST:event_TabelDaerahMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormBakul.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormBakul.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormBakul.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormBakul.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormDaerah().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton HapusButton;
    private javax.swing.JTextField IdDaerahText;
    private javax.swing.JButton KeluarButton;
    private javax.swing.JTextField NamaDaerahText;
    private javax.swing.JButton SimpanButton;
    private javax.swing.JTable TabelDaerah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void tampilDaerah() {
        try {
            List<Daerah> daftarDaerah = DaerahKontrol.getKoneksiDaerah().lihatSeluruhDaerah();

            DaerahTabelModel model = new DaerahTabelModel(daftarDaerah);
            TabelDaerah.setModel(model);

        } catch (SQLException ex) {
            Logger.getLogger(FormDaerah.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
//    private void tampilIdDaerah () {
//        try {
//            IdDaerahText.setText(DaerahKontrol.getKoneksiDaerah().cariIdDaerah());
//        } catch (SQLException ex) {
//            Logger.getLogger(FormDaerah.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
}
